<template>
  <!-- 门店库存盘点 -->
  <div class="flexContent">
    <div class="searchBox">
      <el-form class="demo-form-inline" label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="12" style="display: flex;flex-wrap: wrap;">
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="门店名称">
                  <el-select @click.native="getShopListSelect" v-model="sForm.storeId" popper-class="eloption" clearable
                    style="width: 95%" filterable placeholder="选择门店">
                    <el-option v-for="shop in shopList" :key="shop.storeId" :value="shop.storeId"
                      :label="shop.storeName" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="盘点编号">
                  <el-input v-clearEmoji v-model.trim="sForm.number" style="width: 95%" clearable
                    @keyup.enter.native="serchFn" placeholder="请输入..." />
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="8" :xl="6">
                <el-form-item label="盘点类型">
                  <el-select v-model="sForm.type" popper-class="eloption" clearable style="width: 95%" filterable
                    placeholder="请选择">
                    <el-option value="0" label="期初盘点" />
                    <el-option value="1" label="库存盘点" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xs="12" :sm="12" :md="12" :lg="10" :xl="6">
                <el-form-item label="时间">
                  <el-date-picker clearable v-model="date" style="width: 95%" type="daterange" unlink-panels
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <div class="btnBox">
            <el-button @click.stop="serchFn" class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button @click.stop="resetFn" class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <vxe-table :column-config="{ resizable: true }" border class="tablebox" ref="xTable1"
          :row-config="{ isHover: true }" :scroll-y="{ enabled: true, gt: 21 }" style="width: 100%" height="100%"
          size="medium" align="center">
          <vxe-column type="seq" width="80" title="序号" />
          <vxe-column field="number" title="盘点编号" min-width="180" />
          <vxe-column field="storeName" title="门店名称" min-width="180" />
          <vxe-column field="count" title="盘点商品种类" min-width="150" />
          <vxe-column field="type" title="盘点类型" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.type == '0' ? '期初盘点' : '库存盘点' }}</span>
            </template>
          </vxe-column>
          <vxe-column field="userName" title="盘点人" min-width="150" />
          <vxe-column title="盘点时间" min-width="150">
            <template v-slot="{ row }">
              <span>{{ row.createTime | parseTime }}</span>
            </template>
          </vxe-column>
          <vxe-column field="status" title="状态" min-width="100">
            <template v-slot="{ row }">
              <el-tag v-if="row.status === '0' && row.type == '1'" type="success">正常</el-tag>
              <el-tag v-if="row.status !== '0' && row.type == '1'" type="danger">异常</el-tag>
              <span v-if="row.type == '0'">-</span>
            </template>
          </vxe-column>
          <vxe-column fixed="right" width="200" title="操作">
            <template v-slot="{ row }">
              <el-button type="text" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="shopDetail(row)">详情</el-button>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,-> , sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>

<script>
import { getInventoryList, getShopListSelect } from '@/api/store';
export default {
  name: 'OpeningInventory',
  data() {
    return {
      isSearch: false,
      sForm: {
        storeId: '',
        type: '',
        openTime: '',
        endTime: '',
        number: ''
      },
      shopList: [],
      date: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      inventoryList: [],
    };
  },
  created() {
    this.getInventoryList();
  },
  methods: {
    async getShopListSelect() {
      const res = await getShopListSelect();
      if (res.code === 200) {
        console.log(res);
        // const result = res.data;
        // console.log('shopList', result);
        this.shopList = res.data
        console.log('shopList', this.shopList);
      } else {
        // this.$message.error(res.message);
      }
    },
    async getInventoryList() {
      if (!this.isSearch) {
        this.sForm = {
          storeId: '',
          type: '',
          openTime: '',
          endTime: '',
          number: ''
        }
      }
      const res = await getInventoryList({
        ...this.page,
        ...this.sForm
      });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.inventoryList = result.list;
        this.page.pageSize = result.pageSize;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log('inventoryList', this.inventoryList);
        const $table = this.$refs.xTable1
        $table.loadData(this.inventoryList);
      }
    },
    async serchFn() {
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0
      };
      this.isSearch = true;
      this.sForm.openTime = this.date?.length ? this.date[0] : '';
      this.sForm.endTime = this.date?.length ? this.date[1] : '';
      this.getInventoryList();
    },
    resetFn() {
      this.sForm = {
        storeId: '',
        type: '',
        openTime: '',
        endTime: '',
        number: ''
      }
      this.page.pageNum = 1;
      this.page.pageSize = 20;
      this.date = []
      this.isSearch = false;
      this.getInventoryList();
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getInventoryList();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getInventoryList();
    },
    shopDetail(row) {
      this.$router.push({
        path: "detail",
        query: {
          id: row.id,
        },
      });
    }
  }
};
</script>

<style lang="scss" scoped></style>
